import requests
import re
from util.font import get_font_maps



def download_font_file():
    font_headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
    }

    font_url = "https://www.shixiseng.com/interns/iconfonts/file"
    font_response = requests.get(font_url, headers=font_headers)
    with open("./static/font/file", "wb") as f:
        f.write(font_response.content)

# 获取最新字体
download_font_file()
# 设置字符映射表
font_maps = get_font_maps("./static/font/file")

def parse_field(value):

    pattern = r'&#x([0-9a-fA-F]+)'

    def replace_entity(match):
        hex_code = match.group(1).lower()  # 获取十六进制代码并转为小写

        # 构建映射表键
        char_code = int(hex_code, 16)
        # 在字体映射表中查找
        if char_code in font_maps:
            return font_maps[char_code]
        else:
            # 如果不在映射表中，尝试转换为字符显示
            return chr(char_code)  # 或者返回原字符实体
    # 替换所有字符实体
    result = re.sub(pattern, replace_entity, value)
    return result


headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
}

url = "https://www.shixiseng.com/app/interns/search/v2?build_time=1763372019736&page=2&type=intern&keyword=&area=&months=&days=&degree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E6%9D%AD%E5%B7%9E&internExtend="
response = requests.get(url, headers=headers)

info = {}
for item in response.json()['msg']['data']:
    title = item['name']
    company = item['cname']
    tags = item['industry']
    max_money = item["maxsal"]
    min_money = item["minsal"]
    print(f"工作:{parse_field(title)}, 工资:{parse_field(min_money)}-{parse_field(max_money)}/天, 公司:{company}, 标签:{tags}")
    break

